package me.flyray.crm.core.biz;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.common.crm.request.AccountRequest;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.common.enums.CustomerTypeEnums;
import com.github.wxiaoqi.security.common.util.CurrencyUtils;
import com.github.wxiaoqi.security.common.util.EntityUtils;
import com.github.wxiaoqi.security.common.util.MD5;

import lombok.extern.slf4j.Slf4j;
import me.flyray.crm.core.entity.MerchantAccount;
import me.flyray.crm.core.entity.PersonalAccount;
import me.flyray.crm.core.mapper.MerchantAccountMapper;
import me.flyray.crm.core.mapper.PersonalAccountMapper;

@Slf4j
@Transactional(rollbackFor = Exception.class)
@Service
public class CommonAccountBiz {
	
	@Autowired
	private PersonalAccountMapper personalAccountMapper;
	@Autowired
	private MerchantAccountMapper merchantAccountMapper;
	@Value("${bountyHunter.balanceSaltValue}")
	private String balanceSaltValue;
	
	/**
	 * 创建账户
	 * @return
	 */
	public Map<String, Object> createAccount(AccountRequest accountRequest){
		log.info("创建账户开始.......{}", EntityUtils.beanToMap(accountRequest));
		Map<String, Object> response = new HashMap<String, Object>();
		if(accountRequest.getCustomerType().equals(CustomerTypeEnums.CUST_MERCHANT.getCode())){
			//创建商户账户
			MerchantAccount merchantAccount = new MerchantAccount();
			merchantAccount.setPlatformId(accountRequest.getPlatformId());
			merchantAccount.setMerchantId(accountRequest.getMerchantId());
			merchantAccount.setMerchantType(accountRequest.getMerchantType());
			merchantAccount.setAccountType(accountRequest.getAccountType());
			MerchantAccount selectMerchantAccount = merchantAccountMapper.selectOne(merchantAccount);
			if(null == selectMerchantAccount){
				String accountId = String.valueOf(SnowFlake.getId());
				merchantAccount.setAccountId(accountId);
				merchantAccount.setCcy("CNY");
				merchantAccount.setFreezeBalance(BigDecimal.ZERO);
				merchantAccount.setAccountBalance(BigDecimal.ZERO);
				merchantAccount.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(merchantAccount.getAccountBalance()), balanceSaltValue, "utf-8"));
				merchantAccount.setStatus(accountRequest.getStatus());
				merchantAccount.setCreateTime(new Date());
				merchantAccountMapper.insertSelective(merchantAccount);
				response.put("accountInfo", merchantAccount);
			}else{
				response.put("accountInfo", selectMerchantAccount);
			}
		} else if (accountRequest.getCustomerType().equals(CustomerTypeEnums.CUST_PERSONAL.getCode())) {
			//创建个人账户
			PersonalAccount personalAccount = new PersonalAccount();
			personalAccount.setPlatformId(accountRequest.getPlatformId());
			personalAccount.setPersonalId(accountRequest.getPersonalId());
			personalAccount.setAccountType(accountRequest.getAccountType());
			PersonalAccount selectPersonalAccount = personalAccountMapper.selectOne(personalAccount);
			if(null == selectPersonalAccount){
				String accountId = String.valueOf(SnowFlake.getId());
				personalAccount.setAccountId(accountId);
				personalAccount.setCcy("CNY");
				personalAccount.setAccountBalance(BigDecimal.ZERO);
				personalAccount.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()), balanceSaltValue, "utf-8"));
				personalAccount.setFreezeBalance(BigDecimal.ZERO);
				personalAccount.setStatus(accountRequest.getStatus());
				personalAccount.setCreateTime(new Date());
				personalAccountMapper.insertSelective(personalAccount);
				response.put("accountInfo", personalAccount);
			}else{
				response.put("accountInfo", selectPersonalAccount);
			}
		}
		response.put("code", BizResponseCode.OK.getCode());
		response.put("message", BizResponseCode.OK.getMessage());
		log.info("创建账户结束.......{}", response);
		return response;
	}
	
	/**
	 * 修改账户信息
	 * @return
	 */
	public Map<String, Object> updateAccount(AccountRequest accountRequest){
		log.info("更新个人账户信息开始.......{}", EntityUtils.beanToMap(accountRequest));
		Map<String, Object> response = new HashMap<String, Object>();
		if(accountRequest.getCustomerType().equals(CustomerTypeEnums.CUST_MERCHANT.getCode())){
			//修改商户账户
			MerchantAccount merchantAccount = new MerchantAccount();
			merchantAccount.setPlatformId(accountRequest.getPlatformId());
			merchantAccount.setMerchantId(accountRequest.getMerchantId());
			merchantAccount.setMerchantType(accountRequest.getMerchantType());
			merchantAccount.setAccountType(accountRequest.getAccountType());
			MerchantAccount selectMerchantAccount = merchantAccountMapper.selectOne(merchantAccount);
			if(null != selectMerchantAccount){
				if(!StringUtils.isEmpty(accountRequest.getAccountBalance())){
					selectMerchantAccount.setAccountBalance(new BigDecimal(accountRequest.getAccountBalance()));
					selectMerchantAccount.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(selectMerchantAccount.getAccountBalance()), balanceSaltValue, "utf-8"));
				}
				if(!StringUtils.isEmpty(accountRequest.getStatus())){
					selectMerchantAccount.setStatus(accountRequest.getStatus());
				}
				merchantAccountMapper.updateByPrimaryKey(selectMerchantAccount);
				log.info("更新商户账户信息结束.......{}", EntityUtils.beanToMap(selectMerchantAccount));
				response.put("accountInfo", selectMerchantAccount);
				response.put("code", BizResponseCode.OK.getCode());
				response.put("message", BizResponseCode.OK.getMessage());
			}else{
				response.put("code", BizResponseCode.MER_ACC_NOTEXIST.getCode());
				response.put("message", BizResponseCode.MER_ACC_NOTEXIST.getMessage());
			}
		} else if (accountRequest.getCustomerType().equals(CustomerTypeEnums.CUST_PERSONAL.getCode())) {
			//修改个人账户
			PersonalAccount personalAccount = new PersonalAccount();
			personalAccount.setPlatformId(accountRequest.getPlatformId());
			personalAccount.setPersonalId(accountRequest.getPersonalId());
			personalAccount.setAccountType(accountRequest.getAccountType());
			PersonalAccount selectPersonalAccount = personalAccountMapper.selectOne(personalAccount);
			if(null != selectPersonalAccount){
				if(!StringUtils.isEmpty(accountRequest.getAccountBalance())){
					selectPersonalAccount.setAccountBalance(new BigDecimal(accountRequest.getAccountBalance()));
					selectPersonalAccount.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(selectPersonalAccount.getAccountBalance()), balanceSaltValue, "utf-8"));
				}
				if(!StringUtils.isEmpty(accountRequest.getStatus())){
					selectPersonalAccount.setStatus(accountRequest.getStatus());
				}
				personalAccountMapper.updateByPrimaryKey(selectPersonalAccount);
				log.info("更新个人账户信息结束.......{}", EntityUtils.beanToMap(selectPersonalAccount));
				response.put("accountInfo", selectPersonalAccount);
				response.put("code", BizResponseCode.OK.getCode());
				response.put("message", BizResponseCode.OK.getMessage());
			}else{
				response.put("code", BizResponseCode.PER_ACC_NOTEXIST.getCode());
				response.put("message", BizResponseCode.PER_ACC_NOTEXIST.getMessage());
			}
		}
		return response;
	}

}
